using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using static RTMP.SimpleClient.Helpers.RTMPMessageHelper;

namespace RTMP.SimpleClient.Helpers
{
    public static class MessageSenderHelper
    {
        public static async Task SendAMF0CommandAsync(this Stream stream, byte[] commandData, int chunkSize, CancellationToken cancellationToken = default)
        {
            if (commandData == null || commandData.Length == 0)
            {
                throw new ArgumentException("Invalid command data.");
            }

            //生成RTMP Chunk并发送
            var chunkDataPack = commandData.GetChunkData(RTMPMessageType.AMF0Command, 3, chunkSize);
            await stream.WriteAsync(chunkDataPack, 0, chunkDataPack.Length, cancellationToken);
            await stream.FlushAsync(cancellationToken);
        }
    }
}